layui.config({
    base: '../../../eui/plugins/layui-extend/' //这是你存放拓展模块的根目录
});

layui.use(['form','table'], function () {
	var table = layui.table;
    var form = layui.form;
    var id = getParam("id");
    if(!isNull(id)){
    	io.get('/mm/models/' + id,function(res){
    		var data = res.data;
            var form = layui.form;
            //表单初始赋值
            form.val('lForm', data)
            form.render();
    	});
    }
		table.render({
	          elem: '#fTable'
	        , url: appendCtx('/mm/models/'+ (isNull(id)?0:id) +'/expressions')
	        , page: false
			, height: '500'
			, toolbar: '#tablebar'
			, limit :1000
	        , cols: [[ //表头
	            {field: 'id', title: 'ID',align:'center',width:80}
	            , {field: 'expName', title: '表达式名',align:'left',width:200}
	            , {field: 'priority', title: '优先级',align:'center',width:80}
	            , {field: '', title: '输出类型',align:'center',width:120, templet: 
	                   function(d){
					 return translate("outputType",d.outputType);  //数据字典翻译，具体看base.js中translate方法
		      }}
	            , {field: 'remark', title: '备注',align:'left',width:200}
	            , {field: 'createUserName', title: '创建人',align: 'center',width: 100} 
	            , {field: 'createTime', title: '创建时间',align: 'center',width: 180} 
				,{fixed: 'right', width:120, align:'center', toolbar: '#colbar'}
	        ]]
	        , parseData: function (res) { //res 即为原始返回的数据
	            return {
	                "code": res.code, //解析接口状态
	                "msg": res.msg, //解析提示文本
	                "data": res.data //解析数据列表
	                ,"attr":res.attr
	            };
	        }
	    });
	table.on('tool(fTable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		var layEvent = obj.event;
		  if(layEvent === 'del'){ //删除
		      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
		  }
	});

	table.on('toolbar(fTable)', function(obj){
	  switch(obj.event){
	    case 'add':
			var addExpName=$('#addExpName').val();
			if(isNull(addExpName)){
				layer.msg("请输入表达式名称")
			}
			addExpName=addExpName.replace(/\s+/g,"");
			var allData = table.cache["fTable"];
			for(d in allData){
				var dd=allData[d];
				if(dd.expName===addExpName&&!isNull(dd.id)){
					layer.msg("已经关联该表达式")
					return;
				}
			}
			io.send("/mm/expressions/name","POST",JSON.stringify({"expName":addExpName}),function(res){
				if(isNull(res.data)){
					layer.msg("不存在的表达式")
				}else{
					var allData = table.cache["fTable"];
			        var data =res.data;
			        allData.push(data);
			       //刷新
			        table.reload('fTable', {
			            url: '',
			            data: allData
			        });
				}
			},false,false,false,true);
	    break;
	  };
	});
	
    //监听提交
    form.on('submit(save)', function (form) {
		var allData = table.cache["fTable"];
		var expList = [];
		for(d in allData){
			var dd=allData[d];
			if(!isNull(dd.id)){
				expList.push(dd.id);
			}
		}
        var data=form.field;
		data.expList=expList;
		io.post('/mm/models',JSON.stringify(data),function(res){
	               setTimeout(function(){return closePage();},1000);
	               window.parent.refreshTableData();
		});
		return false;
    });
});